package com.study.newjava8.date;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * SimpleDateFormat线程安全问题
 */
public class SimpleDateFormatDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //异常
        //SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");

        //Java8线程安全
      //  DateTimeFormatter dtf=DateTimeFormatter.ISO_LOCAL_DATE;
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyyMMdd");
        Callable<LocalDate> callable=new Callable<LocalDate>() {
            @Override
            public LocalDate call() throws Exception {
                //return sdf.parse("20161119");
                //return DateFormatThreadLocal.convert("20161218");
                return LocalDate.parse("20191122",dtf);
            }
        };
        ExecutorService executor=  Executors.newFixedThreadPool(10);
        //List<Future<Date>> futures=new ArrayList<>();
        List<Future<LocalDate>> futures=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            futures.add(executor.submit(callable));
        }
        for (Future<LocalDate> future : futures) {
            System.out.println(future.get());
        }

        executor.shutdown();
    }
}
